import threading
import time

# 定义一个函数来计算数字的各位数字之积
def digit_product(number):
    product = 1
    while number > 0:
        digit = number % 10
        product *= digit
        number //= 10
    return product

# 定义一个函数来检查是否满足条件
def check_condition(n):
    n_squared = n * n
    n_plus_1_squared = (n + 1) * (n + 1)
    if digit_product(n_plus_1_squared) == n_squared:
        print(f"Found: n = {n}")

# 定义一个函数来分块检查
def check_range(start, end):
    for n in range(start, end):
        check_condition(n)

# 定义多线程函数
def multi_threaded_search(num_threads, total_range):
    chunk_size = total_range // num_threads
    threads = []

    for i in range(num_threads):
        start = i * chunk_size + 1
        end = (i + 1) * chunk_size + 1 if i != num_threads - 1 else total_range + 1
        thread = threading.Thread(target=check_range, args=(start, end))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

# 主函数
if __name__ == "__main__":
    num_threads = 8  # 可以根据CPU核心数调整线程数
    total_range = 10**6  # 由于10^22太大，这里只检查1到10^6的范围作为示例

    start_time = time.time()
    multi_threaded_search(num_threads, total_range)
    end_time = time.time()

    print(f"Time taken: {end_time - start_time} seconds")